$(function () {
    $("#jqGrid").Grid({
        url: '../modulespdeptstock/list',
        colModel: [
            {label: '仓库名称', name: 'storeHouseName', index: 'STORE_ID', width: 120},
            {label: '型芯名称', name: 'sparePartsName', index: 'MOLD_PS_ID'},
            {label: '图号', name: 'mapNo', index: 'MAP_NO'},
            {label: '库存数', name: 'stockNum', index: 'STOCK_NUM', width: 60},
            {label: '在途数', name: 'transitNum', index: 'TRANSIT_NUM', width: 60},
            {
                label: '明细', width: 60, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.viewStockItem(\"" + row.storeId + "\", \"" + row.moldSpId + "\")'><i class='fa fa-file-text-o'></i></button>";
                }
            },
            {label: '入库数', name: 'stockInNum', hidden: true},
            {label: '出库数', name: 'stockOutNum', hidden: true},
            {label: '仓库ID', name: 'storeId', hidden: true},
            {label: '所属车间', name: 'workshop', hidden: true},
            {label: '型芯备件ID', name: 'moldSpId', hidden: true},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true}
        ]
    });
    // 加载车间列表
    vm.initUserDept();
});

var ztree;
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    callback: {
        beforeCheck: true,
        beforeClick: function (e, treeObj, treeNode) {
            vm.q.workshop = treeObj.deptId;
            vm.reload();
        }
    }
};

let vm = new Vue({
    el: '#rrapp',
    data: {
        treeList: [], // 使用单位列表
        showList: true,
        title: null,
        moduleSpDeptStock: {
            stockInNum: '',
            storeId: '',
            workshop: '',
            remark: '',
            stockOutNum: '',
            transitNum: '',
            stockNum: '',
            moldSpId: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            sparePartsName: '',
            mapNo: '',
            workshop: ''
        }
    },
    methods: {
        viewStockItem: function (storeId, moldSpId) {
            openWindow({
                type: 2,
                area:['100%','100%'],
                title: '<i class="fa fa-bars"></i> 库存明细',
                content: '../module/modulespstockdetail.html?storeId=' + storeId + '&moldSpId=' + moldSpId
            })
        },
        // 加载车间树状结构
        initUserDept: function () {
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=2',
                async: false,
                successCallback: function (r) {
                    vm.treeList = r.list;
                    deptZtree = $.fn.zTree.init($("#storeTree"), deptSetting, vm.treeList);
                    deptZtree.expandAll(true);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        addDemandItem: function () {
            var selectedRowDatas = getSelectedRowDatas("#jqGrid");
            if (selectedRowDatas.length == 0) {
                layer.alert('请至少选择一条型芯库存！', {icon: 5})
                return;
            }
            layer.confirm(`您确认是否将已选中的${selectedRowDatas.length}条型芯添加到本期型芯计划中？`, {
                btn: ['确认', '取消'] //按钮
            }, function () {

                let ids = getSelectedRows("#jqGrid");
                if (ids == null) {
                    return;
                }
                Ajax.request({
                    url: "../modulespdemanditem/addDemandItem",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        // 调整型芯计划
        adjustMoldSpPlan: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 型芯采购明细及寿命要求',
                content: '../module/modulespdemanditem.html',
                cancel: function (index, layero) {
                    layer.close(index);
                    vm.reload();
                }
            })
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.moduleSpDeptStock = {
                stockInNum: '',
                storeId: '',
                workshop: '',
                remark: '',
                stockOutNum: '',
                transitNum: '',
                stockNum: '',
                moldSpId: '',
                id: '',
            };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.moduleSpDeptStock.id == (null || '') ? "../modulespdeptstock/save" : "../modulespdeptstock/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleSpDeptStock),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulespdeptstock/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulespdeptstock/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleSpDeptStock = r.moduleSpDeptStock;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'sparePartsName': vm.q.sparePartsName, 'mapNo': vm.q.mapNo, 'workshop': vm.q.workshop},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                sparePartsName: '',
                mapNo: '',
                workshop: ''
            };
            // 取消选中
            deptZtree.cancelSelectedNode();
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
